/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package tracksuite.bussiness;

import java.lang.reflect.ParameterizedType;
import java.util.List;
import tracksuite.exception.DAOException;
import tracksuite.exception.TrackSuiteException;
import tracksuite.integration.CRUDDAO;
import tracksuite.integration.GenericTO;

/**
 *
 * @author mfloriano
 */
public abstract class GenericBO <TO extends GenericTO,DAO extends CRUDDAO> {

    protected DAO dao;
    private Class<TO> persistentClass;

    public GenericBO() {
        this.persistentClass = (Class<TO>) ((ParameterizedType) getClass().getGenericSuperclass()).getActualTypeArguments()[0];
    }
       
    public void insert(TO to) throws TrackSuiteException {
        try {
            dao.getEntityTransaction().begin();
            dao.save(to);
            dao.getEntityTransaction().commit();
        } catch(Exception e) {
            dao.getEntityTransaction().rollback();
            throw new TrackSuiteException(e.getMessage());
        }
    }
    
    public List<TO> getAll() throws DAOException {
        return dao.findAll(this.persistentClass);
    }
        
}
